
var app = getApp();
var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var wxUtil = require('../../utils/wxUtil.js');

Page(common.extendPage(PageBase, {
    data: {
        dataLoaded: false,
        orderId: '',
        shop: {},
        order: {},
        totalPrice: '',
        logisticsNumber: '',
        shareTitle: "",
        teamid: ''  //团购成团的id
    },

    onLoad: function(options) {
        //debugger;
        var {
            id,
            isgroups
        } = this.options;
        
        if (typeof (options.id) != "undefined" && options.id != "") {
          id = JSON.parse(options.id);
        };
        // if (typeof (options.teamid) != "undefined" && options.teamid != "") {
        //   teamid = JSON.parse(options.teamid);
        // };
        if (typeof (options.isgroups) != "undefined" && options.isgroups == 1) {
          isgroups = JSON.parse(options.isgroups);
        };
        this.setData({
            id: id || "",
            isgroups: isgroups || ""
        });
        this._initPage();
    },

    _initPage: function() {
        var _self = this;
        var {
            id,
            isgroups
        } = this.data;
        var url;
        //debugger;
        if (isgroups == 1){
          url = 'm=plugins&c=groups&a=orderinfo'
        }
        else{
          url = 'm=order&a=info'
        }
        ajax.G({
          url: url,
          data: {
            uid: app.getUid(),
            id: id
          },
          success: (res) => {
            if(res.code == 1){
              _self.setData({
                dataLoaded: true
              });
              util.showWarn(res.msg);
              return;
            }
            //debugger;
            var order = res.data;
            var shareTitle;
            if (typeof(order.goods[0].name) != "undefined"){
              shareTitle = order.goods[0].name;
            }

            //shopInfo.init('shopInfoData', _self);

            _self.setData({
              dataLoaded: true,
              order: order,
              teamid: order.teamid || "",
              shareTitle: shareTitle || 0,
              gid: order.goods[0].goodsid || "",
              isseckill: order.isseckill || ''
              //logisticsNumber: order.expressNum,
              //shop: app.getShop(),
              //groupBuyState: order.groupBuyState
            });
            // 判断是自提的商品并且是已支付就生成提取码
            if (Object.keys(order.address).length == 0 && order.status == 1){
              this._getQRcode();
            }
          },
          fail: () => {
          }
        });

    },

    _getQRcode: function(){
      var url = 'm=index&c=common&a=qrcode';
      var uid = app.getUid();
      var scene = this.data.order.orderno + "," + uid;
      var data = {
        path: "pages/saler/dealtake/index",
        scene: scene
      };
      ajax.G({
        url,
        data,
        success: (data) => {
          //debugger;
          if (data.code == 1) {
            this.setData({
              dataLoaded: true
            });
            util.showWarn(data.msg);
            return
          }
          this.setData({
            qrcode: data.data.url,
            dataLoaded: true
          })
        },
        fail: (data) => {

        }
      })

    },
    

    /**
     * 未支付订单支付
     */
    _payOrder: function(evt) {
        var formId = evt.detail.formId;
        var _self = this;
        var {
            id,
            actType
        } = this.data;
        var url;
        if (this.data.teamid != '' && typeof (this.data.teamid) != 'undefined'){
          url = 'm=order&c=add&a=orderpay';
        }
        else{
          url = 'm=order&c=add&a=orderpay';
        }

        // if (actType == 1) { //团购
        //     url = '/openapi/CActivity/doPayAgain';
        // }

        ajax.G({
          url: url,
          data: {
            uid: app.getUid(),
            id: id
          },
          success: (res) => {
            //debugger;

            // var wxPayParams = JSON.parse(data.result.wxPayParams);
            // _self._package = wxPayParams.package;
            // var prepayArr = _self._package.split('=');
            // common.submitPrepay(prepayArr[1]);


            //debugger
            var result = res.data;
            var wxPayParams = {
              timeStamp: String(result.timestamp),
              nonceStr: result.nonce_str,
              package: result.package,
              paySign: result.sign
            }
            //$this._paySuccess(wxPayParams);


            wxPayParams.success = function () {
             //debugger;
              wx.redirectTo({
                url: '/pages/my/myOrderList/orderList'
              });
            };
            wxPayParams.fail = function () {
              util.showWarn("支付失败")
            }
            wxUtil.wxPay(wxPayParams);

          },
          fail: () => {
          }
        });


        //common.submitForm(formId);
    },

    /**
     * 申请退款
     */
    // _applyRefund: function () {
    //   var self = this;

    //   ajax.P(
    //     '/openapi/CHome/applyOrderRefund',
    //     {
    //       orderId: this.data.orderId,
    //     },
    //     function (data) {
    //       util.alert('您已申请退款，请耐心等待商家处理', function () {
    //         wx.redirectTo({ url: '/pages/orderDetail/orderDetail?orderId=' + self.data.orderId });
    //       });
    //     });
    // },

    _applyRefund: function(evt) {
        var formId = evt.detail.formId;
        var {
            actType,
            groupBuyState
        } = this.data;

        common.submitForm(formId);

        if (actType == 1 && groupBuyState != 1) { //拼团且未成功
            return util.alert('拼团成功方才可申请退款!');
        }
        util.alert('请联系商家退款!');
    },

    _goToLogistics: function(evt) {
        var {
            logisticsNumber,
            orderId,
            order
        } = this.data;
        var orderType = order.order_type;
        var deliveryId = order.delivery_id;

        wx.navigateTo({
                url: '/pages/orderExpressInfo/orderExpressInfo?expressNumber=' + logisticsNumber + '&orderId=' + orderId + '&orderType=' + orderType + '&deliveryId=' + deliveryId
            })
            // common.jump();
    },

    /**
     * 确认收货
     * @return {[type]} [description]
     */
    _confirmTakeGoods(evt) {
        var _self = this;
        var formId = evt.detail.formId;

        util.confirm('您是否确定收货？', function() {
            ajax.P('/openapi/CHome/confirmTakeDelivery', {
                orderId: _self.data.orderId,
            }, function(data) {
                wx.redirectTo({
                    url: '/pages/orderDetail/orderDetail?orderId=' + _self.data.orderId
                })
            });
        });
        //common.submitForm(formId);
    },

    _jump: function(evt) {
        common.jump(evt);
    },

    onShareAppMessage: function (res) {
      return {
        title: this.data.shareTitle,
        path: '/pages/goods/detail2/detail?id=' + this.data.gid,
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
    },
}));